package com.kede.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Agent;
import com.kede.entity.Customer;
import com.kede.entity.Report;
import com.kede.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wrb
 * @since 2020-04-01
 */
@Controller
public class ReportController {

    @Autowired
    ReportService reportService;

    @GetMapping("/report")
    public String report(){
        return "report/report";
    }


    @ResponseBody
    @GetMapping("/reportlist")
    public LayuiTableData list(
            String reporter,
            @RequestParam(value = "page", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ){
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.like("reporter", reporter);
        wrapper.eq("query", 1);

        Page<Report> query = new Page<>();

        query.setCurrent(currentPage);
        query.setSize(limit);

        Page<Report> page = reportService.page(query, wrapper);

        LayuiTableData layuiTableData = new LayuiTableData();
        layuiTableData.setData(page.getRecords());
        layuiTableData.setCount(page.getTotal());

        return layuiTableData;
    }

    @GetMapping("/reportinfo")
    public String index(Integer id, Model model){
        Report report = reportService.getById(id);
        model.addAttribute("report", report);

        return "report/reportinfo";
    }

    @ResponseBody
    @PostMapping("/reportinfo")
    public AjaxMessage update(Report report){
        LocalDateTime now = LocalDateTime.now();
        report.setCreatetime(Timestamp.valueOf(now));
        AjaxMessage message = new AjaxMessage();
        if (reportService.updateById(report)){
            message.setCode(0);
        }else {
            message.setCode(1);
            message.setMsg("更新失败");
        }
        return message;
    }

    @GetMapping("/rdetail")
    public String detail(Integer id,Model model){
       Report report= reportService.getById(id);
        model.addAttribute("report", report);
        return "report/reportinfo";
    }

    @ResponseBody
    @PostMapping("/reportdelete")
    public AjaxMessage delete(Integer id){
        AjaxMessage message = new AjaxMessage();
        if(reportService.removeById(id)){
            message.setCode(0);
            message.setMsg("删除成功");
        }else {
            message.setCode(1);
        }
        return message;
    }


    @GetMapping("/reportadd")
    public String add(){
        return "report/reportadd";
    }

    @ResponseBody
    @PostMapping("report/add")
    public AjaxMessage add(Report report){
        AjaxMessage message = new AjaxMessage();
        if (reportService.save(report)){
            message.setCode(0);
            message.setMsg("添加成功");
        }else {
            message.setCode(1);
        }
        return message;
    }
}
